package com.ssmTmp.api.web;

import com.ssmTmp.api.exception.ZjmsaOaException;
import com.ssmTmp.api.constant.ApiConstant;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by foxx on 2018/6/25
 */
public class ResponseUtil {

    public static ApiResponse done(String msg, Object data) {
        if (StringUtils.isBlank(msg)) {
            return new Done(ApiConstant.RESPONSE_OK_DEFAULT_MSG, data);
        }
        return new Done(msg, data);
    }

    public static ApiResponse done(Object data) {
        return done(ApiConstant.RESPONSE_OK_DEFAULT_MSG, data);
    }

    public static ApiResponse fail(String errMsg) {
        if (StringUtils.isBlank(errMsg)) {
            return new Fail(ApiConstant.RESPONSE_EX_DEFAULT_MSG);
        }
        return new Fail(errMsg);
    }

    public static ApiResponse tokenFail(String errMsg) {
        if (StringUtils.isBlank(errMsg)) {
            return new Fail(ApiConstant.RESPONSE_EX_DEFAULT_MSG);
        }
        return new Fail(100,errMsg);
    }

    public static ApiResponse fail(Exception e) {
        if (e instanceof RuntimeException || e instanceof ZjmsaOaException) {//DataCenter
            return fail(e.getMessage());
        }else{
            return new Fail(ApiConstant.RESPONSE_ERROR_DEFAULT_MSG);
        }
    }

}
